<template>
  <!-- 登录页 -->
  <el-row class="main">
    <el-col :span="12">
      <div class="left">
        <h1>Blog后台管理系统</h1>
      </div>
    </el-col>
    <el-col :span="12">
      <div class="right">
        <div class="login-card">
          <h2 class="welcome">欢迎回来</h2>
          <el-form
            style="max-width: 20vw"
            status-icon
            label-width="auto"
            :rules="rules"
            ref="form"
            :model="formValue"
          >
            <el-form-item label="账号:" prop="username">
              <el-input v-model="formValue.username"></el-input>
            </el-form-item>
            <el-form-item label="密码:" prop="password">
              <el-input v-model="formValue.password"> </el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="code">
              <el-input v-model="formValue.code" placeholder="请输入验证码" clearable>
                <template #append>
                  <el-button @click="getCode">{{
                    codeInputValue === 0 ? '获取邮箱验证码' : codeInputValue
                  }}</el-button>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="margin: 10px auto" @click="handleSubmit"
                >登录</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { adminLogin, getCodeService } from '@/api/Login'
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'

const formValue = ref({
  username: '',
  password: '',
  code: '',
})
const rules = {
  username: [
    { required: true, message: '账号不能为空', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在 3 到 20 个字符之间', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符之间', trigger: 'blur' },
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { min: 6, max: 6, message: '请输入6为长度的验证码', trigger: 'blur' },
  ],
}

const form = ref(null)

const router = useRouter()

const handleSubmit = () => {
  form.value.validate((valid) => {
    if (valid) {
      // 表单校验通过，执行登录逻辑
      try {
        login(formValue.value)
      } catch (e) {
        ElMessage.error(e)
      }
    } else {
      ElMessage('请检查账号密码是否正确')
      return false
    }
  })
}

/**
 * 获取验证码
 */
const codeInputValue = ref(0)
let codeController = 1
const getCode = () => {
  if (codeController === 1) {
    codeController = 0
    getCodeService().then(() => {
      codeInputValue.value = 60
      const timer = setInterval(() => {
        codeInputValue.value--
        if (codeInputValue.value === 0) {
          codeController = 1
          clearInterval(timer)
        }
      }, 1000)
    })
  } else {
    console.log('不可点击')
  }
}

watch(codeInputValue, (newVlaue) => {
  codeInputValue.value = newVlaue
})

/**
 * 登录
 * @param obj
 */
const login = async (obj) => {
  console.log(obj)
  const res = await adminLogin(obj)
  if (res.code === 1) {
    localStorage.setItem('token', res.data.token)
    ElMessage.success('登录成功!')
    router.push('/home')
  }
}
</script>

<style scoped lang="scss">
.main {
  .left {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(149, 200, 232, 0.801);

    h1 {
      color: #fff;
    }
  }
  .right {
    min-height: 100vh;
    display: flex;
    align-items: center;

    .login-card {
      margin: 0 auto;
      padding: 20px;
      width: 25vw;
      min-height: 36vh;
      border-radius: 12px;
      box-shadow: 0 2px 15px #f3f4f5;

      .welcome {
        margin-bottom: 36px;
        text-align: center;
      }
    }
  }
}
</style>